package com.dupt.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.dupt.common.utils.DateUtils;
import com.dupt.common.utils.SecurityUtils;
import com.dupt.common.utils.StringUtils;
import com.dupt.system.domain.*;
import com.dupt.system.domain.param.AssembleMaterialParam;
import com.dupt.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dupt.system.service.IAssembleMaterialService;

/**
 * 组装用料Service业务层处理
 * 
 * @author zhuanyu.sun
 * @date 2025-07-10
 */
@Service
public class AssembleMaterialServiceImpl implements IAssembleMaterialService
{
    @Autowired
    private AssembleMaterialMapper assembleMaterialMapper;
    @Autowired
    private AssemblePlanMapper assemblePlanMapper;
    @Autowired
    private MaterialDetialMapper materialDetialMapper;
    @Autowired
    private MaterialSerialMapper materialSerialMapper;

    /**
     * 查询组装用料
     * 
     * @param assembleMaterialId 组装用料主键
     * @return 组装用料
     */
    @Override
    public AssembleMaterial selectAssembleMaterialByAssembleMaterialId(Long assembleMaterialId)
    {
        return assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialId);
    }

    /**
     * 查询组装用料列表
     *
     assembleMaterialParam     * @return 组装用料
     */
    @Override
    public List<AssembleMaterial> selectAssembleMaterialList(AssembleMaterialParam assembleMaterialParam)
    {
        return assembleMaterialMapper.selectAssembleMaterialList(assembleMaterialParam);
    }

    /**
     * 新增组装用料
     * 
     * @param assembleMaterial 组装用料
     * @return 结果
     */
    @Override
    public int insertAssembleMaterial(AssembleMaterial assembleMaterial)
    {
        return assembleMaterialMapper.insertAssembleMaterial(assembleMaterial);
    }

    /**
     * 确认领料
     * 
     * @param assembleMaterial 组装用料
     * @return 结果
     */
    @Override
    public int updateAssembleMaterial(AssembleMaterial assembleMaterial)
    {
        assembleMaterial.setUpdateTime(DateUtils.getNowDate());
        return assembleMaterialMapper.updateAssembleMaterial(assembleMaterial);
    }

    /**
     * 批量删除组装用料
     * 
     * @param assembleMaterialIds 需要删除的组装用料主键
     * @return 结果
     */
    @Override
    public int deleteAssembleMaterialByAssembleMaterialIds(Long[] assembleMaterialIds)
    {
        return assembleMaterialMapper.deleteAssembleMaterialByAssembleMaterialIds(assembleMaterialIds);
    }

    /**
     * 删除组装用料信息
     * 
     * @param assembleMaterialId 组装用料主键
     * @return 结果
     */
    @Override
    public int deleteAssembleMaterialByAssembleMaterialId(Long assembleMaterialId)
    {
        //根据ID查询相应的物料
//        AssembleMaterial assembleMaterial = assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialId);
//        //根据物料采集ID修改相应的数量
//        MaterialCollect materialCollect = materialCollectMapper.selectMaterialCollectByMaterialCollectId(assembleMaterial.getMaterialCollectId());
//        //根据内码查询物料明细ID
//        List<MaterialDetial> detialList = materialDetialMapper.selectMaterialDetialByMaterialInnerCode(materialCollect.getMaterialInnerCode());
//        //获取所有的序列号ID
//        List<String> materialDetialIds = detialList.stream()
//                .map(MaterialDetial::getMaterialDetialId)
//                .collect(Collectors.toList());
//        //据materialDetialId查询状态为1的所有序列号
//        List<MaterialSerial> serialList = materialSerialMapper.selectMaterialSerialByMaterialDetialIds(materialDetialIds);
//        //修改即时库存表数量
//        if (materialCollect != null) {
//            if (assembleMaterial.getAmount() > serialList.size()) {
//                materialCollect.setUsedAmount(materialCollect.getUsedAmount()-serialList.size());
//                materialCollect.setAvailableAmount(materialCollect.getAvailableAmount() +serialList.size());
//            }else{
//                materialCollect.setUsedAmount(materialCollect.getUsedAmount()-assembleMaterial.getAmount());
//                materialCollect.setAvailableAmount(materialCollect.getAvailableAmount() + assembleMaterial.getAmount());
//            }
//            materialCollect.setUpdateTime(DateUtils.getNowDate());
//            materialCollect.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
//        }
//        //修改物料采集表信息
//        materialCollectMapper.updateMaterialCollect(materialCollect);
//        //删除组装配料表信息
//        return assembleMaterialMapper.deleteAssembleMaterialByAssembleMaterialId(assembleMaterialId);
        return  0;
    }

    @Override
    public int receiveAssembleMaterialByAssembleMaterialId(Long assembleMaterialId, Long receiveCount) {
        //1.根据assembleMaterialId查询组装用料
        AssembleMaterial assembleMaterial = assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialId);
        if (assembleMaterial != null) {
            assembleMaterial.setAmount(assembleMaterial.getAmount() - receiveCount);
            if(assembleMaterial.getAmount() > receiveCount)
                assembleMaterial.setRequisitionStatus("2");
            else
                assembleMaterial.setRequisitionStatus("3");
            return assembleMaterialMapper.updateAssembleMaterial(assembleMaterial);
        }
        return 0;
    }

    @Override
    public int sendBackAssembleMaterialByAssembleMaterialId(Long assembleMaterialId, Long sendCount) {
        //1.根据assembleMaterialId查询组装用料
        AssembleMaterial assembleMaterial = assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialId);
        if (assembleMaterial != null) {
            assembleMaterial.setAmount(assembleMaterial.getAmount() + sendCount);
            return assembleMaterialMapper.updateAssembleMaterial(assembleMaterial);
        }
        return 0;
    }

    /**
     * 确认领料
     *
     * @param assembleMaterialParam 领料信息
     * @return 结果
     */
    @Override
    public int affirmAssembleMaterial(AssembleMaterialParam assembleMaterialParam) {
        //根据ID查询对应物料
        AssembleMaterial assemble = assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialParam.getAssembleMaterialId());
        //根据组装计划ID查询组装计划单号
        AssemblePlan assemblePlan = assemblePlanMapper.selectAssemblePlanByAssembleId(assemble.getAssemblePlanId());
        //修改组装配件信息
        assemble.setPickAmount(Long.valueOf(assembleMaterialParam.getSerialNoes().length));
        assemble.setPickTime(DateUtils.getNowDate());
        assemble.setPickBy(assembleMaterialParam.getPickBy());
        assemble.setRequisitionStatus("2");
        assemble.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        assemble.setUpdateTime(DateUtils.getNowDate());

        List<MaterialSerial> serialList = new ArrayList<>();
        //修改序列号信息
        for(int i = 0; i < assembleMaterialParam.getSerialNoes().length; i++){
            List<MaterialSerial> list = materialSerialMapper.selectMaterialSerialBySerialNo(assembleMaterialParam.getSerialNoes()[i]);
            MaterialSerial serial = list.get(0);
            serial.setCurrentPos(assemblePlan.getAssembleBillNo());
            serial.setUsedId(assemble.getAssembleMaterialId());
            serial.setStatus("2");
            serial.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            serial.setUpdateTime(DateUtils.getNowDate());
            serialList.add( serial);
        }
        materialSerialMapper.batchUpdateMaterialSerial(serialList);
        return assembleMaterialMapper.updateAssembleMaterial(assemble);
    }

    /**
     * 退回领料
     *
     * @param assembleMaterialParam 领料信息
     * @return 结果
     */
    @Override
    public int returnAssembleMaterial(AssembleMaterialParam assembleMaterialParam) {
        //根据ID查询对应物料
        AssembleMaterial assemble = assembleMaterialMapper.selectAssembleMaterialByAssembleMaterialId(assembleMaterialParam.getAssembleMaterialId());
        //根据组装计划ID查询组装计划单号
        AssemblePlan assemblePlan = assemblePlanMapper.selectAssemblePlanByAssembleId(assemble.getAssemblePlanId());
        //修改组装配件信息
        assemble.setReturnAmount(Long.valueOf(assembleMaterialParam.getSerialNoes().length));
        assemble.setReturnTime(DateUtils.getNowDate());
        assemble.setReturnBy(assembleMaterialParam.getPickBy());
        assemble.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        assemble.setUpdateTime(DateUtils.getNowDate());

        List<MaterialSerial> serialList = new ArrayList<>();
        //修改序列号信息
        for(int i = 0; i < assembleMaterialParam.getSerialNoes().length; i++){
            List<MaterialSerial> list = materialSerialMapper.selectMaterialSerialBySerialNo(assembleMaterialParam.getSerialNoes()[i]);
            MaterialSerial serial = list.get(0);
            serial.setCurrentPos(serial.getOriginalPos());
            serial.setUsedId(null);
            serial.setStatus("1");
            serial.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            serial.setUpdateTime(DateUtils.getNowDate());
            serialList.add( serial);
        }
        //修改物料采集表中的数量
//        MaterialCollect materialCollect = materialCollectMapper.selectMaterialCollectByMaterialCollectId(assemble.getMaterialCollectId());
//        materialCollect.setUsedAmount(materialCollect.getUsedAmount() - assemble.getPickAmount());
//        materialCollect.setAvailableAmount(materialCollect.getAvailableAmount() + assemble.getPickAmount());
//        materialCollect.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
//        materialCollect.setUpdateTime(DateUtils.getNowDate());
//
//        //保存或更新数据
//        materialCollectMapper.updateMaterialCollect(materialCollect);
//        materialSerialMapper.batchUpdateMaterialSerial(serialList);
        return assembleMaterialMapper.updateAssembleMaterial(assemble);
    }
}
